package treesNgraphs;

import linkedlist.Node;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by Tzachi on 20/10/2014.
 */
public class ListAllNodesInSameLevel {

    static class RightPointerTreeNode<E> {
        private E value;
        private RightPointerTreeNode<E> leftChild;
        private RightPointerTreeNode<E> rightChild;
        private RightPointerTreeNode<E> rightNeighbour;

        public RightPointerTreeNode(E value) {
            this.value = value;
        }

        public void setLeftChild(RightPointerTreeNode<E> leftChild) {
            this.leftChild = leftChild;
        }

        public void setRightChild(RightPointerTreeNode<E> rightChild) {
            this.rightChild = rightChild;
        }

        public E getValue() {
            return value;
        }

        public RightPointerTreeNode<E> getLeftChild() {
            return leftChild;
        }

        public RightPointerTreeNode<E> getRightChild() {
            return rightChild;
        }

        public boolean hasLeftChild() {
            return leftChild != null;
        }

        public boolean hasRightChild() {
            return rightChild != null;
        }

        public RightPointerTreeNode<E> getRightNeighbour() {
            return rightNeighbour;
        }

        public void setRightNeighbour(RightPointerTreeNode<E> rightNeighbour) {
            this.rightNeighbour = rightNeighbour;
        }

        public boolean hasRightNeighbour() {
            return rightNeighbour != null;
        }
    }

    public <E> void createList(RightPointerTreeNode<E> node) {
        if (node.hasLeftChild() && node.hasRightChild()) {
            RightPointerTreeNode<E> right = node.getRightChild();
            RightPointerTreeNode<E> left = node.getLeftChild();
            left.setRightNeighbour(right);
            if (node.hasRightNeighbour()) {
                right.setRightNeighbour(node.getRightNeighbour().getLeftChild());
            }
            createList(node.getRightChild());
            createList(node.getLeftChild());

        }
    }


}



